(function($)
{
	// This script was written by Steve Fenton
	// http://www.stevefenton.co.uk/Content/Jquery-Side-Content/
	// Feel free to use this jQuery Plugin
	
	var classModifier = "";
	var sliderCount = 0;
	var sliderWidth = "400px";
	
	var attachTo = "rightside";
	
	var totalPullOutHeight = 0;
	
	function ToggleSlider () {
		var thisId = classModifier + "_" + jQuery(this).attr("rel");
		var thisPulloutId = thisId + "_pullout";
		var showSlider = true;
		
		// Reset previous sliders
		for (var i = 0; i < sliderCount; i++) {
			var sliderId = classModifier + "_" + i;
			var pulloutId = sliderId + "_pullout";
			
			// Only reset it if it is shown
			if (jQuery("#" + sliderId).width() > 0) {

				if (sliderId == thisId) {
					// They have clicked on the open slider, so we'll just close it
					showSlider = false;
				}

				// Close the slider
				jQuery("#" + sliderId).animate({
					width: "0px"
				}, 1500);
				
				// Reset the pullout
				if (attachTo == "leftside") {
					jQuery("#" + pulloutId).animate({
						left: "0px"
					}, 1500);
				} else {
					jQuery("#" + pulloutId).animate({
						right: "0px"
					}, 1500);
				}
			}
		}
		
		if (showSlider) {
			// Open this slider
			jQuery("#" + thisId).animate({
				width: sliderWidth
			}, 1500);
			
			// Move the pullout
			if (attachTo == "leftside") {
				jQuery("#" + thisPulloutId).animate({
					left: sliderWidth
				}, 1500);
			} else {
				jQuery("#" + thisPulloutId).animate({
					right: sliderWidth
				}, 1500);
			}
		}
		
		return false;
	};

	jQuery.fn.sidecontent = function (settings) {
	
		var config = { classmodifier: "sidecontent", attachto: "rightside", width: "300px", opacity: "0.8", pulloutpadding: "5", textdirection: "vertical" };
		
		if (settings) {
			$.extend(config, settings);
		}
		
		return this.each(function () {
			
			classModifier = config.classmodifier;
			sliderWidth = config.width;
			attachTo = config.attachto;
			
			var sliderId = classModifier + "_" + sliderCount;
			var sliderTitle = config.title;
			
			// Get the title for the pullout
			sliderTitle = jQuery(this).attr("title");
			
			// Start the totalPullOutHeight with the configured padding
			if (totalPullOutHeight == 0) {
				totalPullOutHeight += parseInt(config.pulloutpadding);
			}

			if (config.textdirection == "vertical") {
				var newTitle = "";
				var character = "";
				for (var i = 0; i < sliderTitle.length; i++) {
					character = sliderTitle.charAt(i).toUpperCase();
					if (character == " ") {
						character = "&nbsp;";
					}
					newTitle = newTitle + "<span>" + character + "</span>";
				}
				sliderTitle = newTitle;
			}
			
			// Wrap the content in a slider and add a pullout			
			jQuery(this).wrap('<div class="' + classModifier + '" id="' + sliderId + '"></div>').wrap('<div style="width: ' + sliderWidth + '"></div>');
			jQuery("#" + sliderId).before('<div class="' + classModifier + 'pullout" id="' + sliderId + '_pullout" rel="' + sliderCount + '">' + sliderTitle + '</div>');
			
			if (config.textdirection == "vertical") {
				jQuery("#" + sliderId + "_pullout span").css({
					display: "block",
					textAlign: "center"
				});
			}
			
			// Hide the slider
			jQuery("#" + sliderId).css({
				position: "absolute",
				overflow: "hidden",
				top: "0",
				width: "0px",
				zIndex: "1",
				opacity: config.opacity
			});
			
			// For left-side attachment
			if (attachTo == "leftside") {
				jQuery("#" + sliderId).css({
					left: "0px",
				});
			} else {
				jQuery("#" + sliderId).css({
					right: "0px",
				});
			}
			
			// Set up the pullout
			jQuery("#" + sliderId + "_pullout").css({
				position: "absolute",
				top: totalPullOutHeight + "px",
				zIndex: "1000",
				cursor: "pointer",
				opacity: config.opacity
			}).live("click", ToggleSlider);
			
			var pulloutWidth = jQuery("#" + sliderId + "_pullout").width();
			
			// For left-side attachment
			if (attachTo == "leftside") {
				jQuery("#" + sliderId + "_pullout").css({
					left: "0px",
					width: pulloutWidth + "px"
				});
			} else {
				jQuery("#" + sliderId + "_pullout").css({
					right: "0px",
					width: pulloutWidth + "px"
				});
			}
			
			totalPullOutHeight += parseInt(jQuery("#" + sliderId + "_pullout").height());
			totalPullOutHeight += parseInt(config.pulloutpadding);
			
			var suggestedSliderHeight = totalPullOutHeight + 30;
			if (suggestedSliderHeight > jQuery("#" + sliderId).height()) {
				jQuery("#" + sliderId).css({
					height: suggestedSliderHeight + "px"
				});
			}
			
			sliderCount++;
		});
		
		return this;
	};
})(jQuery);